package com.LFLWZ.serviceImpl;

import com.LFLWZ.beans.POJO.ClassroomCourse;
import com.LFLWZ.beans.POJO.ClassroomCourseDetails;
import com.LFLWZ.beans.POJO.UserCourse;
import com.LFLWZ.beans.Vo.ClassroomCourseVo;
import com.LFLWZ.mapper.ClassroomCourseDetailsMapper;
import com.LFLWZ.mapper.ClassroomCourseMapper;
import com.LFLWZ.mapper.UserCourseMapper;
import com.LFLWZ.service.ClassroomCourseDetailsService;
import com.LFLWZ.utils.EmptyUtils;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * (ClassroomCourseDetails)表服务实现类
 *
 * @author lujum
 * @since 2020-12-03 01:41:32
 */
@Service
public class ClassroomCourseDetailsServiceImpl implements ClassroomCourseDetailsService {

    @Autowired
    private ClassroomCourseDetailsMapper classroomCourseDetailsMapper;
    @Autowired
    private UserCourseMapper userCourseMapper;
    @Autowired
    private ClassroomCourseMapper classroomCourseMapper;

    @Override
    public List<ClassroomCourseDetails> queryAll(ClassroomCourseDetails classroomCourseDetails) {
        return classroomCourseDetailsMapper.queryAll(classroomCourseDetails);
    }
    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ClassroomCourseDetails queryById(Integer id) {
        return this.classroomCourseDetailsMapper.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<ClassroomCourseDetails> queryAllByLimit(int offset, int limit) {
        return this.classroomCourseDetailsMapper.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param classroomCourseDetails 实例对象
     * @return 实例对象
     */
    @Override
    public ClassroomCourseDetails insert(ClassroomCourseDetails classroomCourseDetails) {
        this.classroomCourseDetailsMapper.insert(classroomCourseDetails);
        return classroomCourseDetails;
    }

    /**
     * 修改数据
     *
     * @param classroomCourseDetails 实例对象
     * @return 实例对象
     */
    @Override
    public ClassroomCourseDetails update(ClassroomCourseDetails classroomCourseDetails) {
        this.classroomCourseDetailsMapper.update(classroomCourseDetails);
        return this.queryById(classroomCourseDetails.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.classroomCourseDetailsMapper.deleteById(id) > 0;
    }

    /**
     * 计算以学习人数
     * @param sid  课程编号
     * @param id  课程id
     * @return
     */
    @Override
    public boolean countTheNumberOfLearners(Integer sid, Integer id,Integer uid) {
        UserCourse userCourse=new UserCourse(uid,id);
        List<UserCourse> userCourses= userCourseMapper.queryAll(userCourse);
        if (EmptyUtils.isEmpty(userCourses)){
            int line=userCourseMapper.insert(userCourse);
            ClassroomCourseVo classroomCourse= classroomCourseMapper.queryById(id);
            classroomCourse.setStudynumber(classroomCourse.getStudynumber()+1);
            if (line>0){
                classroomCourseMapper.update(classroomCourse);
            }
        }
        ClassroomCourseDetails classroomCourseDetails=new ClassroomCourseDetails();
        classroomCourseDetails.setNumber(sid);
        classroomCourseDetails.setCourseid(id);
        List<ClassroomCourseDetails> list= classroomCourseDetailsMapper.queryAll(classroomCourseDetails);
        boolean b=false;
        if (EmptyUtils.isNotEmpty(list)){
            list.get(0).setPlaynumber(list.get(0).getPlaynumber()+1);
            int line=classroomCourseDetailsMapper.update(list.get(0));
            if (line>0){
                b=true;
            }
        }
        return b;
    }
}